<?php

namespace plugin\webman\xiangyaota;
use support\Db;
use GatewayWorker\Lib\Gateway;
use support\Redis;
use plugin\webman\common\Common;

// 降妖塔公共方法
class Method
{
    // 根据id获取npc名字
    static function get_npc_name_byid($npcid){
       $npc = self::npcinfo();
       $xiabiao = $npc[$npcid] ? $npc[$npcid] : '';
       if($npc[$npcid]){
            return $npc[$npcid]['name'];
       }else{
           return 1005;
       }
       
    }
    // 根据名字获取npc的id
    static function get_npc_id_byname($npcname){
        $npcinfo = self::npcinfo();

        foreach ($npcinfo as $k => $v){
            if($v['name'] == $npcname){
                return $v['id'];
            }
        }
    }

    // 妖怪属性
    static function npcinfo(){

        $data=[
            ["id"=>"0","name"=>"牛头"],
            ["id"=>"1","name"=>"雷鸟人"],
            ["id"=>"2","name"=>"六耳猕猴"],
            ["id"=>"3","name"=>"马面"],
            [ "id"=>"4","name"=>"猪妖"],
            [ "id"=>"5","name"=>"猪妖"],
            [ "id"=>"6","name"=>"羊头怪"],
            [ "id"=>"7","name"=>"青蛇精"]
        ];
       return $data;
    } 

    // 投注数据记录到redis中
    static function setxytdata($data){
        // 设定房间人数，投注就代表了用户在此房间内

        self::setuser_toroom($data['uid'],$data['expect'],$data['npcid']);
        $key = 'xyt'.$data['expect'];
        $betinfo= Common::getRedis($key);
        if(!$betinfo){
            $npclist = self::npcinfo();
            foreach($npclist as $k=>$v){
                if($data['npcid']==$k){
                    $v['allbet']=(int)$data['coin'];
                }else{
                    $v['allbet']=(int)0;
                }
                $v['nums']=self::getroomnums($data['expect'],$k);
                $betinfo[] = $v;
            }
            $cun=json_encode($betinfo);
            $betinfo= Common::getRedis($key);
            Common::setRedis($key,$cun);
        }else{
            $qu = json_decode($betinfo,true);
            // var_dump($qu[0]);die;
            switch ($data['npcid']) {
                case '0':
                    $qu[0]['allbet']=$qu[0]['allbet']+$data['coin'];
                    $qu[0]['nums']=self::getroomnums($data['expect'],0);
                    break;
                case '1':
                    $qu[1]['allbet']=$qu[1]['allbet']+$data['coin'];
                    $qu[0]['nums']=self::getroomnums($data['expect'],1);
                    break;
                case '2':
                    $qu[2]['allbet']=$qu[2]['allbet']+$data['coin'];
                    $qu[0]['nums']=self::getroomnums($data['expect'],2);
                    break;
                case '3':
                    $qu[3]['allbet']=$qu[3]['allbet']+$data['coin'];
                    $qu[0]['nums']=self::getroomnums($data['expect'],3);
                    break;
                case '4':
                    $qu[4]['allbet']=$qu[4]['allbet']+$data['coin'];
                    $qu[0]['nums']=self::getroomnums($data['expect'],4);
                    break;
                case '5':
                    $qu[5]['allbet']=$qu[5]['allbet']+$data['coin'];
                    $qu[0]['nums']=self::getroomnums($data['expect'],5);
                    break;
                case '6':
                    $qu[6]['allbet']=$qu[6]['allbet']+$data['coin'];
                    $qu[0]['nums']=self::getroomnums($data['expect'],6);
                    break;
                case '7':
                    $qu[7]['allbet']=$qu[7]['allbet']+$data['coin'];
                    $qu[0]['nums']=self::getroomnums($data['expect'],7);
                    break;
            }
           
            $cun=json_encode($qu);
            Common::setRedis($key,$cun);
        }


        //删除前两期的redis缓存
        $key3 = 'xyt'.($data['expect']-2);
        Common::delRedis($key3);
    
    }

    static function tt(){
        $key = 'a';
        $data = Common::setRedis($key, [2]);
        // var_dump(Common::getRedis($key));
        $roomnum = [100, 1002, 1003];
        Redis::sAdd($key, $roomnum);
        $count = Redis::sCard($key);
        echo "数组中有 " . $count . " 个值";
    }

    // 获取房间内的人数总数
    static function getroomnums($expect,$roomid){
        $num = Db::table("wa_xyt_touzhu")->where("expect",$expect)->where("npcid",$roomid)->count('uid');
        // var_dump($num,$roomid,$expect);
        return $num;
    }

    // 设定用户在哪个房间,并处理房间人数
    static function setuser_toroom($uid,$expect,$roomid){
        $key = $uid."_".$expect;
        $info = Common::getRedis($key,$roomid);
        if(!$info){
            $info = Common::setRedis($key,$roomid);
        }else{
            if($info!=$roomid){
                Common::delRedis($key,$info);
                $info = Common::setRedis($key,$roomid);
                //查询redis中某值的数量  redis::sCard($key)
            }
        }

    }



    static function getuserallbet($uid,$expect){
        $all = Db::table("wa_xyt_touzhu")->where(['uid'=>$uid,'expect'=>$expect])->sum("coin");
        return $all;
    }

    // 获取所有投注信息列表
    static function getallbetlist($expect){
        $key = 'xyt'.$expect;
        $betinfo= Common::getRedis($key);
        if(!$betinfo){
            $npclist = self::npcinfo();
            foreach($npclist as $k=>$v){
                $v['allbet']=(int)0;
                $v['nums']=self::getroomnums($expect,$k);
                $betinfo[] = $v;
            }
            $cun=json_encode($betinfo);
            Common::setRedis($key,$cun);
            $betinfo= Common::getRedis($key);
            $betinfo2=json_decode($betinfo,true);
            return $betinfo2;
        }else{
            $betinfo=json_decode($betinfo,true);
            return $betinfo;
        }
        
       
    }

}